import Service from '@/api/httpAxios'

export interface eventType {
    offset?: number
    limit?: number
    project_id: string
    region: string
    event_type?: string
    start_time?: string
    end_time?: string
}
export interface eventBasic {
    offset?: number
    limit?: number
    project_id: string
    region: string
    resource_type: string
    trace_state: string
    event_type: string
    record_time: string
    id: string
}
export interface deleteList {
    project_id: string
    event_id: string
}
export interface subjectData {
    project_id: string
    notice_trace_status: string
}
export interface createInformation {
    project_id: string
    name: string
    display_name: string
    enterprise_project_id: string
    notice_trace_status: string
}
export interface information {
    protocol: string
    endpoint: string
    remark: string
}
export interface AddSubscriptionRequest {
    project_id?: string
    urn?: string
    UpdateSubscription: information
}
export interface theme {
    project_id?: string
}
export interface subscription {
    project_id?: string
    urn?: string
}

class eventList {
    getEventList = (obj: eventType, obj2: any) => {
        let pid
        obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
        const url = `/v1/${pid}/event`
        let params: { [key: string]: string | number } = {}
        for (const key in obj2) {
            if (obj2[key]) {
                params[key] = obj2[key]
            }
        }
        params['event_type'] = obj.event_type as any
        params['start_time'] = obj.start_time as any
        params['end_time'] = obj.end_time as any
        if (obj.offset) params['offset'] = obj.offset - 1
        if (obj.limit) params['limit'] = obj.limit
        return Service.get<any>(url, {
            params
        })
    }
    deleteEvent = (obj: deleteList) => {
        let pid
        obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
        const url = `/v1/${pid}/event/${obj.event_id}`
        return Service.delete<any>(url)
    }
    getNotification = (obj: eventType) => {
        let pid
        obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
        const url = `/v1/${pid}/topic`
        return Service.get<any>(url, {
            params: {
                offset: obj.offset ? obj.offset - 1 : obj.offset,
                limit: obj.limit,
            }
        })
    }
    createTopic = (obj: createInformation) => {
        let pid
        obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
        const url = `/v1/${pid}/topic`
        return Service.post<any>(url, obj)
    }
    updataTheme = (obj: subjectData) => {
        let pid
        obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
        const url = `/v1/${pid}/topic`
        return Service.put<any>(url, obj)
    }
    addSubscription = (obj: AddSubscriptionRequest) => {
        let pid
        obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
        const url = `/v1/${pid}/topic/subscriptions/${obj.urn}`
        return Service.post<any>(url, obj.UpdateSubscription)
    }
    deletemessage = (obj: theme) => {
        let pid
        obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
        const url = `/v1/${pid}/topic`
        return Service.delete<any>(url)
    }
    deletesubscribe = (obj: subscription) => {
        let pid
        obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
        const url = `/v1/${pid}/topic/subscriptions/${obj.urn}`
        return Service.delete<any>(url)
    }

}

export default new eventList()
